package net.recursv.motific.samples.at;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class SimpleMidlet extends MIDlet {
    protected void startApp() throws MIDletStateChangeException {
        setScreen(new PaintedScreen(this));
    }

    protected void pauseApp() {
        setScreen(null);
    }

    protected void destroyApp(boolean b) throws MIDletStateChangeException {
        pauseApp();
    }

    private void setScreen(Canvas screen) {
        Display.getDisplay(this).setCurrent(screen);
    }
}

class PaintedScreen extends Canvas implements CommandListener {
    private MIDlet _midlet;
    private int _oneX = 10;
    private int _oneY = 10;
    private int _twoX = 50;
    private int _twoY = 50;
    private int _dim = 40;
    private static final int RED = 0xff0000;
    private static final int WHITE = 0xffffff;
    private static final String ONE = "One";
    private static final String TWO = "Two";
    private static final int TOP_LEFT = Graphics.TOP | Graphics.LEFT;
    private int _textOffset = 5;
    private FocusState _one = new OneFocused();
    private FocusState _two = new TwoFocused();
    private FocusState _state = _one;

    public PaintedScreen(MIDlet midlet) {
        _midlet = midlet;
        addCommand(new Command("Exit", Command.EXIT, 0));
        setCommandListener(this);
    }

    protected void paint(Graphics g) {
        g.setColor(0);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(RED);
        g.fillRect(_oneX, _oneY, _dim, _dim);
        g.setColor(WHITE);
        g.drawRect(_oneX, _oneY, _dim, _dim);
        g.drawString(ONE, _oneX + _textOffset, _oneY + _textOffset, TOP_LEFT);

        g.setColor(RED);
        g.fillRect(_twoX, _twoY, _dim, _dim);
        g.setColor(WHITE);
        g.drawRect(_twoX, _twoY, _dim, _dim);
        g.drawString(TWO, _twoX + _textOffset, _twoY + _textOffset, TOP_LEFT);
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command.getCommandType() == Command.EXIT)
            _midlet.notifyDestroyed();
    }

    protected void keyPressed(int keycode) {
        _state.keyPressed(getGameAction(keycode));
        _state = _state == _one ? _two : _one;
        repaint();
    }

    interface FocusState {
        void keyPressed(int keycode);
    }

    class OneFocused implements FocusState {
        public void keyPressed(int action) {
            switch (action) {
                case Canvas.UP:
                    _oneY = _oneY - _dim * 2;
                    if (_oneY < 0)
                        _oneY = getHeight() - _dim;
                    break;
                case Canvas.DOWN:
                    _oneY += _dim * 2;
                    if (_oneY > getHeight())
                        _oneY = 0;
                    break;
                case Canvas.LEFT:
                    _oneX -= _dim * 2;
                    if (_oneX < 0)
                        _oneX = getWidth() - _dim;
                    break;
                case Canvas.RIGHT:
                    _oneX += _dim * 2;
                    if (_oneX > getWidth())
                        _oneX = 0;
                    break;
            }
        }
    }

    class TwoFocused implements FocusState {
        public void keyPressed(int action) {
            switch (action) {
                case Canvas.UP:
                    _twoY = _twoY - _dim * 2;
                    if (_twoY < 0)
                        _twoY = getHeight() - _dim;
                    break;
                case Canvas.DOWN:
                    _twoY += _dim * 2;
                    if (_twoY > getHeight())
                        _twoY = 0;
                    break;
                case Canvas.LEFT:
                    _twoX -= _dim * 2;
                    if (_twoX < 0)
                        _twoY = getWidth() - _dim;
                    break;
                case Canvas.RIGHT:
                    _twoX += _dim * 2;
                    if (_twoX > getWidth())
                        _twoY = 0;
                    break;
            }
        }
    }
}

